package oop.core.ai;

import java.util.Random;

import oop.core.entity.NPC;
import oop.core.map.GameMap;
import oop.core.med.NPCMovementMediator;

/**
 * AI behavior: occasionally wander and then wander back after some time
 *
 */
public class CivilianAI extends AI {
	private int lastDirection; // moves back to original position after wandering
	
	/**
	 * CivilianAI constructor.
	 * @param movementMediator
	 * @param gameMap
	 * @param npc
	 */
	public CivilianAI(NPCMovementMediator movementMediator, GameMap gameMap, NPC npc){
		super(movementMediator, gameMap, npc);
		this.lastDirection = 5;
	}
	
	/**
	 * Overide move to implement the civilian behavior.
	 */
	public void move() {
		// wanders every so often (1/50 clocks) otherwise just stands there
		Random rand = new Random();
		int n = rand.nextInt(500);
		if(n==1){
			int direction = 5;
			if(lastDirection == 5){
				randomMove();
			}
			else{
				direction = getInverse(lastDirection);
				lastDirection = 5;
				getMovementMediator().moveEntity(direction); 
			}
		}
	}
	
	/**
	 * Get the opposite direction to a given direction.
	 * @param n
	 * @return
	 */
	public int getInverse(int direction){
		int inverse = 0;
		switch(direction){
			case 1: inverse = 9;
			case 2: inverse = 8;
			case 3: inverse = 7;
			case 4: inverse = 6;
			case 6: inverse = 4;
			case 7: inverse = 3;
			case 8: inverse = 2;
			case 9: inverse = 1; 
			default: inverse = 2; 
		}
		return inverse;
	}
	
	/**
	 * get/set methods
	 */
	public int getLastDirection() {
		return lastDirection;
	}
	public void setLastDirection(int lastDirection) {
		this.lastDirection = lastDirection;
	}
}
